package com.ymatou.iapi.liveseller.testcase;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.CreateProductVideoBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.service.CreateProductVideoCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.livetv.ProductVideo;
import com.ymttest.database.model.livetv.ProductVideoExplain;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*创建录制任务(开始录制)Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CreateProductVideo{
	private static CreateProductVideoBean createproductvideoBean;
	private static CreateProductVideoCall createproductvideoCall;
	private static LiveRoomWapper liveRoomWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建录制任务(开始录制)Test");
	}

	@Before
	public void caseUp() {
		createproductvideoBean=new CreateProductVideoBean();
		createproductvideoCall=new CreateProductVideoCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createproductvideoCall.getOpurl());
		liveRoomWapper=null; 
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateProductVideo_001() {
		Logger.start(true,"创建直播录制，成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 4000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-createLiveRecord-Normal");
			String taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			
			createproductvideoBean.setLiveId(livedto.getId());
			createproductvideoBean.setUserId(sellerId);
			createproductvideoBean.setProductId(productId);
			
			createproductvideoCall.setData(createproductvideoBean);
			createproductvideoCall.callService();			
			Logger.verifyEquals("200", createproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),1 , "验证ExplainStatus");
			Thread.sleep(3000);
			
			Map map = new ImDefaultWapper().selectImNotifyByToUserId(sellerId).get(0);
			System.out.println(map);
			
			Logger.verifyEquals(1,liveRoomWapper.selectLiveproductdot(livedto.getId(),productId).get(0).getDotResource(),"验证打点信息录制");
			LiveRoomCallService.SetLiveProductTop(livedto.getId(), productId);
			Logger.verifyEquals(1,liveRoomWapper.selectLiveproductdot(livedto.getId(),productId).get(0).getDotResource(),"验证打点信息录制");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateProductVideo_002() {
		Logger.start(true,"直播间不存在，不能录制");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 4000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-createLiveRecord-Normal");
			String taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			
			createproductvideoBean.setLiveId(123);
			createproductvideoBean.setUserId(sellerId);
			createproductvideoBean.setProductId(productId);
			
			createproductvideoCall.setData(createproductvideoBean);
			createproductvideoCall.callService();			
			Logger.verifyEquals("201", createproductvideoCall.getString("Code"), "验证code");
			Logger.verifyEquals("直播不存在！", createproductvideoCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateProductVideo_003() {
		Logger.start(true,"创建直播录制，重复的商品进行 录制成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 4000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-createLiveRecord-Normal");
			String taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			
			createproductvideoBean.setLiveId(livedto.getId());
			createproductvideoBean.setUserId(sellerId);
			createproductvideoBean.setProductId(productId);
			
			createproductvideoCall.setData(createproductvideoBean);
			createproductvideoCall.callService();			
			taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			createproductvideoCall.callService();			
			Logger.verifyEquals("201", createproductvideoCall.getString("Code"), "验证code");
			Logger.verifyIsContains("正在讲解录制", createproductvideoCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateProductVideo_004() {
		Logger.start(true,"开始直播，求讲解后。主播进行录播，会给求讲解的人推送消息");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 4000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			
			LiveRoomCallService.AskProductVideo(livedto.getId(), productId,123);
			
			//增加mock，消除存量数据
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-createLiveRecord-Normal");
			String taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			
			createproductvideoBean.setLiveId(livedto.getId());
			createproductvideoBean.setUserId(sellerId);
			createproductvideoBean.setProductId(productId);
			
			createproductvideoCall.setData(createproductvideoBean);
			createproductvideoCall.callService();			
			Logger.verifyEquals("200", createproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),1 , "验证ExplainStatus");
			Thread.sleep(3000);
			Logger.comment("查询买手"+new ImDefaultWapper().selectImNotifyByToUserId(sellerId).get(0));
			Map map = new ImDefaultWapper().selectImNotifyByToUserId(123).get(0);
			Logger.comment(map.toString());
//			Logger.verifyNotEquals(-1, map.toString().indexOf(productId), "验证查询的push包含商品id");
//			Logger.verifyNotEquals(-1, map.toString().indexOf(livedto.getId()+""), "验证查询的push包含直播id");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateProductVideo_005() {
		Logger.start(true,"创建直播录制，成功、计数器+1");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 1秒钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 4000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(livedto.getId(), sellerId, productId);
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), sellerId);
			//增加mock，消除存量数据
			
			LinkedHashMap<String,Object> m=new LinkedHashMap<>();
			m.put("mock", "mock-enable");
			m.put("mockId-1", TestDataManager.getRandomUUID().toString());
			m.put("mockApi-1", "liveseller-createLiveRecord-Normal");
			String taskId = TestDataManager.getRandomNum(9);
			m.put("mockParam-1-taskId", taskId);
			PayGateWayCallService.addMock(createproductvideoCall, m);
			
			createproductvideoBean.setLiveId(livedto.getId());
			createproductvideoBean.setUserId(sellerId);
			createproductvideoBean.setProductId(productId);
			
			createproductvideoCall.setData(createproductvideoBean);
			
			Integer counter = CounterCallService.getSingleValueByLiveId(livedto.getId());
			createproductvideoCall.callService();	
			Integer counter1 = CounterCallService.getSingleValueByLiveId(livedto.getId());
			Logger.verifyEquals(counter+1, counter1, "添加商品之后，计数器+1");
			
			
			Logger.verifyEquals("200", createproductvideoCall.getString("Code"), "验证code");
			ProductVideoExplain pv = liveRoomWapper.selectProductvideoExplain(productId);
			Logger.verifyEquals(pv.getUserId(),sellerId , "验证UserId");
			Logger.verifyEquals(pv.getTaskId()+"",taskId , "验证TaskId");
			Logger.verifyEquals(pv.getLiveId(),livedto.getId() , "验证LiveId");
			Logger.verifyEquals(pv.getExplainStatus(),1 , "验证ExplainStatus");
			Thread.sleep(3000);
			Map map = new ImDefaultWapper().selectImNotifyByToUserId(sellerId).get(0);
			System.out.println(map);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}